package com.qf.controller;/*
 *@Description:
 *@author:赵健
 *@Date:zjzzz 2022/9/28 20:15
 */

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/set")
    public String set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
        stringRedisTemplate.opsForValue().set(key, value);
        return "success";
    }

    @PostMapping("/setobj")
    public String set(@RequestBody User user) {
        redisTemplate.opsForValue().set(user.getName(), user);
        try {
            stringRedisTemplate.opsForValue().set(user.getName(), objectMapper.writeValueAsString(user));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "success";
    }

    @PostMapping("/set2")
    public String set2(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
        return "success";
    }

    @PostMapping("/setobj2")
    public String set2(@RequestBody User user) {
        redisTemplate.opsForValue().set(user.getName(),user);
        return "success";
    }
}
